﻿using Common.Lib.Dto;
using Common.Lib.Service;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.Todo.ViewModels
{
    public class LoginViewModel : ViewModelBase, IDialogAware
    {
        /// <summary>
        /// 登录  
        /// </summary>
        public LoginViewModel(ILoginService loginService)
        {
            this.loginService = loginService;
        }

        #region 账号

        /// <summary>
        /// 账号
        /// </summary>
        private string _account;

        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get { return _account; }
            set { SetProperty(ref _account, value); }
        }

        #endregion 账号 

        #region 密码

        /// <summary>
        /// 密码
        /// </summary>
        private string _passWord;

        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord
        {
            get { return _passWord; }
            set { SetProperty(ref _passWord, value); }
        }

        #endregion 密码 


        #region 选中页

        /// <summary>
        /// 选中页
        /// </summary>
        private int _selectedIndex = 0;

        /// <summary>
        /// 登录
        /// </summary>
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { SetProperty(ref _selectedIndex, value); }
        }

        #endregion 选中页 


        #region 注册信息

        /// <summary>
        /// 注册信息
        /// </summary>
        private RegisterUserDto _registerDto;

        /// <summary>
        /// 注册信息
        /// </summary>
        public RegisterUserDto RegisterDto
        {
            get { return _registerDto; }
            set { SetProperty(ref _registerDto, value); }
        }

        #endregion 注册信息 


        #region 返回

        /// <summary>
        /// 返回 - 命令
        /// </summary>
        private DelegateCommand _goBackCmd;

        /// <summary>
        /// 返回 - 命令
        /// </summary>
        public DelegateCommand GoBackCmd =>
            _goBackCmd ?? (_goBackCmd = new DelegateCommand(ExecuteGoBack));

        /// <summary>
        /// 执行 - 返回
        /// </summary>    
        void ExecuteGoBack()
        {
            SelectedIndex = 0;
        }

        #endregion 返回

        #region 前进

        /// <summary>
        /// 前进 - 命令
        /// </summary>
        private DelegateCommand _goForwardCmd;

        /// <summary>
        /// 前进 - 命令
        /// </summary>
        public DelegateCommand GoForwardCmd =>
            _goForwardCmd ?? (_goForwardCmd = new DelegateCommand(ExecuteGoForward));

        /// <summary>
        /// 执行 - 前进
        /// </summary>    
        void ExecuteGoForward()
        {
            SelectedIndex = 1;
            RegisterDto ??= new RegisterUserDto();
        }

        #endregion 前进

        #region 登录

        /// <summary>
        /// 登录 - 命令
        /// </summary>
        private DelegateCommand _loginCmd;

        /// <summary>
        /// 登录 - 命令
        /// </summary>
        public DelegateCommand LoginCmd =>
            _loginCmd ?? (_loginCmd = new DelegateCommand(ExecuteLogin));

        /// <summary>
        /// 执行 - 登录
        /// </summary>    
        async void ExecuteLogin()
        {
            try
            {
                if (!string.IsNullOrEmpty(Account) && !string.IsNullOrEmpty(PassWord))
                {
                   var apiRes = await loginService.LoginAsync(this.Account, this.PassWord);
                    if(apiRes?.Status == true)
                    {
                        //登录成功
                        MessageBox.Show("登录成功", "信息", MessageBoxButton.OK, MessageBoxImage.Information);
                        RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                    }
                    else
                    {
                        MessageBox.Show(apiRes?.Message,"登录失败",MessageBoxButton.OK,MessageBoxImage.Error);
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }

        #endregion 登录

        #region 注册

        /// <summary>
        /// 注册 - 命令
        /// </summary>
        private DelegateCommand _registerCmd;
        private readonly ILoginService loginService;

        /// <summary>
        /// 注册 - 命令
        /// </summary>
        public DelegateCommand RegisterCmd =>
            _registerCmd ?? (_registerCmd = new DelegateCommand(ExecuteRegister));

        /// <summary>
        /// 执行 - 注册
        /// </summary>    
        async void ExecuteRegister()
        {
            if(RegisterDto != null)
            {
                if(!string.IsNullOrEmpty(RegisterDto.Account) && !string.IsNullOrEmpty(RegisterDto.PassWord) && !(string.IsNullOrEmpty(RegisterDto.ConfirmPassWord)))
                {
                    if(RegisterDto.PassWord != RegisterDto.ConfirmPassWord)
                    {
                        MessageBox.Show($"密码与确认密码不一致，请查证！", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    var apiRes = await loginService.RegisterAsync(new UserDto()
                    {
                        Account = RegisterDto.Account,
                        PassWord = RegisterDto.PassWord,                        
                    });
                    if(apiRes?.Status == null)
                    {
                        MessageBox.Show($"注册成功！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                        return;
                    }
                    else
                    {
                        MessageBox.Show($"注册失败！{apiRes.Status}", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show($"请将信息输入完整","提示",MessageBoxButton.OK,MessageBoxImage.Error);
                    return;
                }
            }
        }

        #endregion 注册


        public string Title { get; set; } = "ToDo";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }
    }
}
